Skip to content

fix: resolve LogSource for getClass results on Scala 3 - #3609

Merged
He-Pin merged 1 commit into
apache:mainfrom
pjfanning:fix-logsource-scala3-1538
Oct 10, 2026
Merged

He-Pin merged 1 commit into
apache:mainfrom
pjfanning:fix-logsource-scala3-1538

Conversation

@pjfanning

Copy link
Copy Markdown
Member

Motivation

On Scala 3, Logging(system, getClass) fails to compile with Cannot find LogSource for Class[? <: Foo]. getClass returns a wildcard class type, which Scala 3 does not match against LogSource.fromAnyClass[T]: LogSource[Class[T]]. Scala 2.13 accepts the same code.

Modification

  • object LogSource now extends a new LogSourceCompat trait.
  • The Scala 3 LogSourceCompat adds a lower priority fromAnySubClass[T]: LogSource[Class[? <: T]] implicit. The Scala 2.13 version is empty, because Scala 2 reports that implicit as ambiguous with fromAnyClass.
  • fromAnyClass is unchanged. Changing its signature to LogSource[Class[? <: T]] also works, but MiMa reports an IncompatibleSignatureProblem.
  • Added LogSourceSpec.

Result

Logging(system, getClass) and LogSource(getClass, system) compile on Scala 3.

Tests

  • actor-tests/Test/testOnly org.apache.pekko.event.LogSourceSpec on Scala 3.3.8 without the fix: compile error Cannot find LogSource for Class[? <: ...LogsWithGetClass]
  • actor-tests/Test/testOnly org.apache.pekko.event.LogSourceSpec org.apache.pekko.event.LoggerSpec with the fix: passed on 2.13.18 and 3.3.8
  • actor/mimaReportBinaryIssues: passed on 2.13.18 and 3.3.8
  • scalafmt run on changed files

References

Fixes #1538

Motivation:
On Scala 3, `Logging(system, getClass)` fails to compile with
"Cannot find LogSource for Class[? <: Foo]". `getClass` returns a
wildcard class type, which Scala 3 does not match against
`LogSource.fromAnyClass[T]: LogSource[Class[T]]`. Scala 2.13 accepts it.

Modification:
- Make `object LogSource` extend a new `LogSourceCompat` trait.
- The Scala 3 `LogSourceCompat` adds a lower priority
  `fromAnySubClass[T]: LogSource[Class[? <: T]]` implicit; the Scala 2.13
  version is empty because Scala 2 reports that implicit as ambiguous
  with `fromAnyClass`.
- `fromAnyClass` is unchanged; changing its signature instead fails MiMa
  with an IncompatibleSignatureProblem.
- Add `LogSourceSpec`.

Result:
`Logging(system, getClass)` and `LogSource(getClass, system)` compile
on Scala 3.

Tests:
- `actor-tests/Test/testOnly org.apache.pekko.event.LogSourceSpec` on
  Scala 3.3.8 without the fix: compile error "Cannot find LogSource for
  Class[? <: ...LogsWithGetClass]"
- `actor-tests/Test/testOnly org.apache.pekko.event.LogSourceSpec
  org.apache.pekko.event.LoggerSpec` with the fix: passed on 2.13.18
  and 3.3.8
- `actor/mimaReportBinaryIssues`: passed on 2.13.18 and 3.3.8
- scalafmt run on changed files

References:
Fixes apache#1538
@pjfanning pjfanning added this to the 2.0.0-M5 milestone Oct 9, 2026

@He-Pin He-Pin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The inherited-member lower priority keeps fromAnyClass winning for exact Class[T] while fromAnySubClass covers Scala 3's Class[? <: T] from getClass, and the empty 2.13 trait avoids the Scala 2 ambiguity. Verified fromClass overrides getClazz and uses simpleName, matching the new spec's expectations on both Scala versions.

@He-Pin
He-Pin merged commit b8625a6 into apache:main Oct 10, 2026
10 checks passed
@pjfanning
pjfanning deleted the fix-logsource-scala3-1538 branch October 10, 2026 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LoggingAdapter with ActorSystem and class broken in scala 3

2 participants